import { asyncRoutes, constantRoutes } from '@/router'

const state = {
  // 路由表, 标记当前用户所拥有的所有路由
  routes: constantRoutes // 默认静态路由表
}

const mutations = {
  // otherRoutes登录成功后, 需要添加的新路由
  setRoutes(state, otherRoutes) {
    //   静态路由的基础上,累计加上其他权限路由
    state.routes = [...constantRoutes, ...otherRoutes]
  }
}
const actions = {
  filterRoutes(context, menus) {
    // menu与动态路由规则过滤  otherRoutes
    const otherRoutes = asyncRoutes.filter(item => {
      if (menus.includes(item.children[0].name)) {
        return true
      } else {
        return false
      }
    })
    context.commit('setRoutes', otherRoutes)
    return otherRoutes
  }
}

// 默认导出
export default {
  namespaced: true, // 开启密闭空间
  state,
  mutations,
  actions
}
